package com.cc.network

import android.Manifest
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.Network
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.core.app.ActivityCompat
import com.cc.networkmonitor.NetStateMonitor
import com.cc.networkmonitor.NetWorkUtils
import com.cc.networkmonitor.WifiStateMonitor

class MainActivity : AppCompatActivity() {
    lateinit var tvNetWork: TextView
    lateinit var tvWifi: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (ActivityCompat.shouldShowRequestPermissionRationale(
                this,
                Manifest.permission.READ_PHONE_STATE
            )
        ) {
            init()
        } else {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.READ_PHONE_STATE),
                1
            )
        }
        tvNetWork = findViewById(R.id.tvNetwork)
        tvWifi = findViewById(R.id.tvWifi)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            init()
        }
    }

    private fun init() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            //startAuto：activity退出自动
            NetStateMonitor()
                .startAuto(this, object : ConnectivityManager.NetworkCallback() {
                    override fun onAvailable(network: Network) {
                        super.onAvailable(network)
                        tvNetWork.text =
                            "网络已连接:${NetWorkUtils.getConnectTypeName(this@MainActivity)}"
                    }

                    override fun onLost(network: Network) {
                        super.onLost(network)
                        tvNetWork.text = "网络已断开"
                    }
                })
            NetStateMonitor().start(this, ConnectivityManager.NetworkCallback())
        }
        WifiStateMonitor().startAuto(this, object : WifiStateMonitor.WifiStateCallback() {
            override fun onWifiDisable() {
                super.onWifiDisable()
                tvWifi.text = "WIFI不可用"
            }

            override fun onWifiEnabled() {
                super.onWifiEnabled()
                tvWifi.text = "WIFI已打开"
            }

            override fun onWifiEnabling() {
                super.onWifiEnabling()
                tvWifi.text = "WIFI正在打开"
            }

            override fun onWifiDisabling() {
                super.onWifiDisabling()
                tvWifi.text = "WIFI正在关闭"
            }
        })

    }
}